# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Miloš Komarčević <miloskomarcevic@aim.com>

_realname=exiv2
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.28.5
pkgrel=1
pkgdesc="Exif/IPTC/Xmp C++ metadata library and tools (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://exiv2.org/'
msys2_repository_url="https://github.com/Exiv2/exiv2"
msys2_references=(
  'archlinux: exiv2'
  "cpe: cpe:/a:exiv2:exiv2"
  "cygwin: exiv2"
  "gentoo: media-gfx/exiv2"
)
license=('spdx:GPL-2.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-python")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libinih"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=("https://github.com/Exiv2/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('e1671f744e379a87ba0c984617406fdf8c0ad0c594e5122f525b2fb7c28d394d')

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  #shared
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DCONAN_AUTO_INSTALL=OFF \
      -DEXIV2_ENABLE_VIDEO=ON \
      -DEXIV2_ENABLE_NLS=ON \
      -DEXIV2_BUILD_SAMPLES=OFF \
      -DEXIV2_ENABLE_WEBREADY=ON \
      -DEXIV2_ENABLE_CURL=ON \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      -S "${_realname}-${pkgver}" \
      -B "build-shared-${MSYSTEM}"

  ${MINGW_PREFIX}/bin/cmake --build "build-shared-${MSYSTEM}"

  #static
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DCMAKE_CXX_FLAGS="-DCURL_STATICLIB" \
      -DCMAKE_C_FLAGS="-DCURL_STATICLIB" \
      -DBUILD_SHARED_LIBS=OFF \
      -DCONAN_AUTO_INSTALL=OFF \
      -DEXIV2_ENABLE_VIDEO=ON \
      -DEXIV2_ENABLE_NLS=ON \
      -DEXIV2_BUILD_SAMPLES=OFF \
      -DEXIV2_ENABLE_WEBREADY=ON \
      -DEXIV2_ENABLE_CURL=ON \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      -S "${_realname}-${pkgver}" \
      -B "build-static-${MSYSTEM}"

  ${MINGW_PREFIX}/bin/cmake --build "build-static-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install "build-static-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install "build-shared-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
